package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.ProtectScopeService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.ProtectScopeDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ProtectScope.
 */
@RestController
@RequestMapping("/api")
public class ProtectScopeResource {

    private final Logger log = LoggerFactory.getLogger(ProtectScopeResource.class);

    private static final String ENTITY_NAME = "protectScope";

    private final ProtectScopeService protectScopeService;

    public ProtectScopeResource(ProtectScopeService protectScopeService) {
        this.protectScopeService = protectScopeService;
    }

    /**
     * POST  /protect-scopes : Create a new protectScope.
     *
     * @param protectScopeDTO the protectScopeDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new protectScopeDTO, or with status 400 (Bad Request) if the protectScope has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/protect-scopes")
    @Timed
    public ResponseEntity<ProtectScopeDTO> createProtectScope(@RequestBody ProtectScopeDTO protectScopeDTO) throws URISyntaxException {
        log.debug("REST request to save ProtectScope : {}", protectScopeDTO);
        if (protectScopeDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new protectScope cannot already have an ID")).body(null);
        }
        ProtectScopeDTO result = protectScopeService.save(protectScopeDTO);
        return ResponseEntity.created(new URI("/api/protect-scopes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /protect-scopes : Updates an existing protectScope.
     *
     * @param protectScopeDTO the protectScopeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated protectScopeDTO,
     * or with status 400 (Bad Request) if the protectScopeDTO is not valid,
     * or with status 500 (Internal Server Error) if the protectScopeDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/protect-scopes")
    @Timed
    public ResponseEntity<ProtectScopeDTO> updateProtectScope(@RequestBody ProtectScopeDTO protectScopeDTO) throws URISyntaxException {
        log.debug("REST request to update ProtectScope : {}", protectScopeDTO);
        if (protectScopeDTO.getId() == null) {
            return createProtectScope(protectScopeDTO);
        }
        ProtectScopeDTO result = protectScopeService.save(protectScopeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, protectScopeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /protect-scopes : get all the protectScopes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of protectScopes in body
     */
    @GetMapping("/protect-scopes")
    @Timed
    public ResponseEntity<List<ProtectScopeDTO>> getAllProtectScopes(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ProtectScopes");
        Page<ProtectScopeDTO> page = protectScopeService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/protect-scopes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /protect-scopes/:id : get the "id" protectScope.
     *
     * @param id the id of the protectScopeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the protectScopeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/protect-scopes/{id}")
    @Timed
    public ResponseEntity<ProtectScopeDTO> getProtectScope(@PathVariable Long id) {
        log.debug("REST request to get ProtectScope : {}", id);
        ProtectScopeDTO protectScopeDTO = protectScopeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(protectScopeDTO));
    }

    /**
     * DELETE  /protect-scopes/:id : delete the "id" protectScope.
     *
     * @param id the id of the protectScopeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/protect-scopes/{id}")
    @Timed
    public ResponseEntity<Void> deleteProtectScope(@PathVariable Long id) {
        log.debug("REST request to delete ProtectScope : {}", id);
        protectScopeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/protect-scopes?query=:query : search for the protectScope corresponding
     * to the query.
     *
     * @param query the query of the protectScope search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/protect-scopes")
    @Timed
    public ResponseEntity<List<ProtectScopeDTO>> searchProtectScopes(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of ProtectScopes for query {}", query);
        Page<ProtectScopeDTO> page = protectScopeService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/protect-scopes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
